package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-if-array-can-be-sorted/description/https://leetcode.cn/problems/find-if-array-can-be-sorted/description/
 * @create 2025/11/3 09:35
 * @since JDK17
 */

public class Solution02 {
    public boolean canSortArray(int[] nums) {
        int preMax = 0, n = nums.length;
        for (int i = 0, j = 0; j < n; i = j) {
            int x = Integer.bitCount(nums[i]), mx = 0;
            while (j < n && x == Integer.bitCount(nums[j])) {
                if (preMax > nums[j]) {
                    return false;
                }
                mx = Math.max(mx, nums[j++]);
            }
            preMax = mx;
        }

        return true;
    }
}
